Sounds visualization with Python

Librosa library was used for the visualization. To obtain the visualization (spectrogram) the short-time Fourier transform was applied.

In this work the sound from a flute is visualised. The sound file is available for free freesound.org

In [1]:
# ref: https://musicinformationretrieval.com/index.html

import librosa
%matplotlib inline
import matplotlib.pyplot as plt
import librosa.display
In [2]:
# x are the variable values in the audio array, sr is the sample rate (common sampling rate is 44100 Hz)

x, sr = librosa.load('flute.wav')
In [3]:
print(x.shape)
print(sr)
(831744,)
22050
In [4]:
#Audio array visualization
plt.figure(figsize=(14, 5))
librosa.display.waveplot(x, sr=sr)
Out[4]:
<matplotlib.collections.PolyCollection at 0x1f69a049a58>
In [5]:
#Spectrogram visualization 
#short time Fourier Transform. Musical signals are highly non-stationary, 
#i.e., their statistics change over time. It would be rather meaningless 
#to compute a single Fourier transform over an entire 10-minute song.
# X is obtained by computing the Fourier transform for successive frames in a signal.
X = librosa.stft(x) 

Xdb = librosa.amplitude_to_db(abs(X))
plt.figure(figsize=(14, 5))
librosa.display.specshow(Xdb, sr=sr, x_axis='time', y_axis='hz')

#from the following spectrogram can be detemined spectral features such as pitch, pitch class, harmonics, percussive etc.
Out[5]:
<matplotlib.axes._subplots.AxesSubplot at 0x1f69bc01cf8>
In [6]:
import IPython.display as ipd
In [7]:
ipd.Audio('flute.wav')
Out[7]:
In [ ]: